<template>
  <section>
    <a class='detail-back' @click="back"></a>
    <div class="cover">
      <img :src="thumb">
    </div>
    <div class="info">
      <p class="title">{{name}}</p>
      <p class="amount">{{price}} {{currency_code}}</p>
    </div>
    <div class="detail mt20">
      <div class="title">商品详情</div>
      <div class="content" v-html="desc"></div>
    </div>
    <div class="sticky-box">
      <nuxt-link
        :to="{name:'pay-confirm',query:{id:id,type:'detail'}}"
        class="btn btn-primary btn-block"
      >立即下单</nuxt-link>
    </div>
    <div class="btn btn-block"></div>
  </section>
</template>

<script>
let ROUTE_FROM ;
export default {
  validate({ query ,from}) {
    ROUTE_FROM = from;
    // 如果userInfo , form为空，表示没有经过上一步操作非法进入页面
    return !!query.id;
  },
  async asyncData(context) {
    const { query, app, redirect } = context;

    try {
      let { data: rs } = await app.$axios.post("?ct=goods&ac=detail", {
        id: query.id
      });
      if (rs.code == 0) {
        return rs.data;
      } else {
        app.$toast(rs.msg).then(() => {
          if (context.from.name !== "good-detail") {
            //有上一页
            app.router.back();
          } else {
            redirect("/");
          }
        });
      }
    } catch (e) {
      app.$toast("请求错误");
      app.$error(e);
    }
    return { id: query.id };
  },
  methods:{
    back(){
      if (ROUTE_FROM.name !== "good-detail") {
          //有上一页
          this.$router.back();
        } else {
          this.$router.replace('/')
        }
    }
  }
};
</script>


<style lang="less" scoped>
.cover {
  position: relative;
  width: 100%;
  height: 400px;
  overflow: hidden;
  background-color: #c0c3cc;
  &:after {
    content: "";
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: linear-gradient(
      to top,
      rgba(0, 0, 0, 0.1) 10px,
      rgba(0, 0, 0, 0)
    );
  }
  img {
    display: block;
    width: 100%;
    height: auto;
  }
}
.info {
  padding: 34px 20px;
  background-color: #fff;
  display: flex;
  overflow: hidden;
  .title {
    width: 100%;
    line-height: 42px;
    font-size: 32px;
    color: #292929;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    /* autoprefixer: off */
    -webkit-box-orient: vertical;
    /* autoprefixer: on */
    overflow: hidden;
  }
  .amount {
    font-size: 28px;
    line-height: 42px;
    color: #f05521;
    flex-shrink: 0;
    flex-grow: 0;
    width: 270px;
    margin-left: 10px;
    text-align: right;
  }
}
.detail {
  background-color: #fff;
  .title {
    padding: 30px 20px;
    font-size: 32px;
    line-height: 1;
    color: #292929;
    border-bottom: 1px solid #ededf0;
  }
  .content {
    padding: 30px 20px;
    font-size: 30px;
    line-height: 42px;
    word-break: break-word;
    overflow: hidden;
    color: #666;

    /deep/ img {
      display: block;
      width: 100% !important;
      height: auto;
      margin-bottom: 20px;
      background-color: #edeef0;
    }

    /deep/ p,
    /deep/ div,
    /deep/ span,
    /deep/ pre {
      margin-bottom: 20px;
      // word-wrap: break-word;
      text-align: justify;
      word-break: break-word;
      word-break: break-all;
      text-indent: 0 !important;
    }
    /deep/ a {
      color: #337ab7;
    }
    /deep/ video,
    /deep/ iframe,
    /deep/ embed {
      display: block;
      max-width: 100%;
      // height:auto;
      margin: auto;
    }
    /deep/ video,
    /deep/ embed {
      background-color: rgb(45, 45, 45);
    }
  }
}
.sticky-box {
  .btn-primary {
    position: fixed;
    bottom: 0;
    left: 0;
    border-radius: 0;
  }
}
.detail-back {
  position:fixed;
  z-index:5;
  top:20px;
  left:20px;
  width:50px;
  height:50px;
  background-image:url('~assets/img/detail-back.png');
  background-size: 100%;
  border-radius: 50%;
  overflow: hidden;
}
</style>

